//
//  Bird.cpp
//  Birds
//
//  Created by Denveryee on 14-2-10.
//
//

#include "Bird.h"
#include "cocos-ext.h"
#include "VisibleRect.h"
#pragma  comment(lib,"libBox2d.lib")
#pragma  comment(lib,"libExtensions.lib")
using namespace extension;
using namespace cocos2d;
#include "SimpleAudioEngine.h"

USING_NS_CC;

bool Bird::init(){
    
    //flypig
    CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("birdAction0.png", "birdAction0.plist", "birdAction.json");
    
    //flypig
    CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("BirdActions0.png", "BirdActions0.plist", "BirdActions.json");
    
    return true;
}

void Bird::createFlyBird(int score){
    
    if(score%2 == 0){
        flyBird = cocos2d::extension::CCArmature::create("BirdActions");
    }else{
        flyBird = cocos2d::extension::CCArmature::create("birdAction");
    }
    
	flyBird->getAnimation()->playByIndex(0);
    //armature->setAnchorPoint(ccp(0.5, -0.1));
	//armature->setScale(0.2f);
	//armature->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y+25));
    
    flyBird->setPosition( ccp(-1000.0*(rand()%101)/100.0, 630) );


    CCActionInterval*  actionBy1 = CCJumpBy::create(flySpeed-score/1000.00, ccp(200, 0), 100+70*((rand()%101)/100.0), 1);
    
    CCActionInterval*  actionBy2 = CCJumpBy::create(flySpeed-score/1000.00, ccp(200, 0), 150+20*((rand()%101)/100.0), 1);
    
    CCActionInterval* fly = CCSpawn::create(actionBy1,
                                            CCCallFunc::create(this, callfunc_selector(Bird::fly)),
                                            NULL);
    
    CCActionInterval* fly1 = CCSpawn::create(actionBy2,
                                             CCCallFunc::create(this, callfunc_selector(Bird::fly)),
                                             NULL);
    
    jump = CCSequence::create(fly,
                              fly,
                              fly,
                              fly1,
                              NULL);
    
    feJump = CCRepeatForever::create(jump);
    
    flyBird->runAction(feJump);

}

Bird::Bird(void){
    birdKick = false;
    birdDying = false;
    angry = 0;
    killScore = 1;
    flySpeed = 0.6;
}


Bird::~Bird(void){

}

void Bird::getAngry(){
    
    if(angry < 4)
        angry++;
    
    switch (angry) {
        case 1:
            flyBird->setColor(ccc3(255, 127, 80));
            break;
        case 2:
            flyBird->setColor(ccc3(255, 99, 71));
            break;
        case 3:
            flyBird->setColor(ccc3(255, 0, 0));
            break;
        case 4:
            flyBird->setColor(ccc3(227, 23, 13));
            killScore = 5;
            break;
        default:
            break;
    }
    
}

void Bird::kickBack(){
    birdKick = true;
    
    if(angry == 0)
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("kick.wav");
    if(angry == 1)
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("kick2.wav");
    if(angry == 2)
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("kick3.wav");
    if(angry == 3)
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("kick4.wav");
    if(angry == 4)
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("kick5.wav");
    
    //碰撞后生气
    getAngry();
    
    flyBird->stopAction(feJump);
    
    CCFiniteTimeAction*  action = CCSequence::create(
                                                     CCSpawn::create(
                                                                     CCMoveTo::create(0.5, ccp(flyBird->getPositionX()-200-(angry>0?100*angry:0), 630)),
                                                                     CCRotateBy::create(0.5, -360),
                                                                     NULL),
                                                     CCCallFunc::create(this, callfunc_selector(Bird::kickCallback)),
                                                     NULL);
    
    flyBird->runAction(action);

}

void Bird::kickFront(){
    birdKick = true;

    CCFiniteTimeAction*  action = CCSequence::create(
                                                     CCSpawn::create(
                                                                     CCMoveBy::create(0.5, ccp(200+(angry>0?100*angry:0), 0)),
                                                                     CCRotateBy::create(0.5, 360),
                                                                     NULL),
                                                     CCCallFunc::create(this, callfunc_selector(Bird::kickCallback)),
                                                     NULL);
    
    flyBird->runAction(action);
}

void Bird::kickCallback()
{
    birdKick = false;
    
    if(angry > 0){

        int far = 225;
        switch (angry) {
            case 1:
                
                break;
            case 2:
                far = 250;
                break;
            case 3:
                far = 300;
                break;
            case 4:
                far = 350;
                break;
            default:
                break;
        }
        
        CCActionInterval*  actionBy1 = CCJumpBy::create(flySpeed-(angry<4?angry/10.0:0.35), ccp(far, 0), 50+70*((rand()%101)/100.0), 1);
     
        CCActionInterval*  actionBy2 = CCJumpBy::create(flySpeed-(angry<4?angry/10.0:0.35), ccp(far, 0), 100+20*((rand()%101)/100.0), 1);
     
     CCActionInterval* fly = CCSpawn::create(actionBy1,
                                                CCCallFunc::create(this, callfunc_selector(Bird::fly)),
                                                NULL);
        
     CCActionInterval* fly1 = CCSpawn::create(actionBy2,
                                                 CCCallFunc::create(this, callfunc_selector(Bird::fly)),
                                                 NULL);
        
        jump = CCSequence::create(fly,
                                  fly,
                                  fly,
                                  fly1,
                                  NULL);
     
        feJump = CCRepeatForever::create(jump);
        
        flyBird->runAction(feJump);
    }
}

void Bird::fly(){
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("heng.mp3");
}